package com.liang.lingdong.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.liang.lingdong.common.BaseResponse;
import com.liang.lingdong.common.ErrorCode;
import com.liang.lingdong.common.ResultUtils;
import com.liang.lingdong.exception.BusinessException;
import com.liang.lingdong.model.auth.dto.UserJoinDto;
import com.liang.lingdong.model.auth.dto.UserLoginByEmailDto;
import com.liang.lingdong.model.auth.dto.UserLoginDto;
import com.liang.lingdong.model.auth.dto.UserSendEmailDto;
import com.liang.lingdong.model.auth.vo.AuthCodeVo;
import com.liang.lingdong.model.auth.vo.UserLoginVo;
import com.liang.lingdong.model.user.vo.UserVo;
import com.liang.lingdong.service.AuthService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    /**
     * 获取验证码
     * @return 验证码返回
     */
    @GetMapping("/code")
    public BaseResponse<AuthCodeVo> getAuthCode() {
        AuthCodeVo authCodeVo = authService.getAuthCode();
        return ResultUtils.success(authCodeVo);
    }

    /**
     * 加入我们
     * @param userJoinDto 用户加入信息
     * @return 结果
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> join(@RequestBody @Validated UserJoinDto userJoinDto) {
        if (userJoinDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean flag = authService.userJoin(userJoinDto);
        return ResultUtils.success(flag);
    }


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginVo> login (@RequestBody @Validated UserLoginDto userLoginDto) {
        if (userLoginDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserLoginVo userLoginVo = authService.userLogin(userLoginDto);
        return ResultUtils.success(userLoginVo);
    }

    /**
     * 邮箱登录
     * @param userLoginByEmailDto 登录参数
     * @return 结果
     */
    @PostMapping("/login/email")
    public BaseResponse<UserLoginVo> login (@RequestBody @Validated UserLoginByEmailDto userLoginByEmailDto) {
        if (userLoginByEmailDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserLoginVo userLoginVo = authService.userLoginByEmail(userLoginByEmailDto);
        return ResultUtils.success(userLoginVo);
    }


    /**
     * 发送邮箱验证码
     * @param userSendEmailDto 发送邮箱验证码参数
     * @return 结果
     */
    @PostMapping("/send/email")
    public BaseResponse<String> sendEmail(@RequestBody @Validated UserSendEmailDto userSendEmailDto) {
        if (userSendEmailDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String flag = authService.sendEmail(userSendEmailDto);
        return ResultUtils.success(flag);
    }


    /**
     * 获取用户信息
     * @return 结果
     */
    @GetMapping("/info")
    public BaseResponse<UserVo> queryLoginUserInfo() {
        UserVo userVo = authService.loginUserInfo();
        return ResultUtils.success(userVo);
    }


}
